<template>
	<view class="page">
		<view class="page-tabs">
			<view class="fo">
				<scroll-view
					scroll-x="true"
					class="scroll">
					<view class="tabs">
						<view
							v-for="(item,i) in tabs"
							:key="item.val"
							@click="changeTabs(i)"
							:class="{
								'item': true,
								'is-active': orderType == i
							}">
							<view class="cell">
								<text>{{item}}</text>
							</view>
						</view>
					</view>
				</scroll-view>
				<view
					class="filter"
					@click="open('timePopup')">
					<image
						class=""
						src="https://img.2024csnl.com/static/2.x/a101.png">
					</image>
					<text>{{ '筛选' || time }}</text>
				</view>
			</view>
			<view
				v-if="false"
				class="state">
				<!-- <view class="item is-active">
					<view class="cell">
						<view class="num">
							<text>12</text>
						</view>
						<view class="label">
							<text>待付款</text>
						</view>
					</view>
				</view> -->
				<!-- <view class="item">
					<view class="cell">
						<view class="num">
							<text>999</text>
						</view>
						<view class="label">
							<text>待核销</text>
						</view>
					</view>
				</view> -->
				<!-- <view class="item">
					<view class="cell">
						<view class="num">
							<text>10998</text>
						</view>
						<view class="label">
							<text>待发货</text>
						</view>
					</view>
				</view> -->
				<!-- <view class="item">
					<view class="cell">
						<view class="num">
							<text>12</text>
						</view>
						<view class="label">
							<text>待收货</text>
						</view>
					</view>
				</view> -->
			</view>
		</view>
		
		<view class="page-list">
			<view class="list">
				<view
					v-for="(item, index) in list"
					:key="index"
					:class="{
						'item': true,
						'is-fukuan': index == 0,
						'is-hexiao': index == 1,
						'is-fahuo': index == 2,
						'is-shouhuo': index == 3
					}">
					<view class="cell">
						<view class="cover">
							<image :src="item.binfo.logo" mode=""></image>
						</view>
						<view class="exp">
							<view class="t1">
								<view class="name">
									<text>收货人：</text>
									<text>{{ item.linkman || '-' }}</text>
								</view>
								<view class="state">
									<text>{{ item.name }}</text>
								</view>
							</view>
							<view class="row">
								<text>下单时间：</text>
								<text>{{ item.createtime_txt }}</text>
							</view>
							<view class="row">
								<text>本单金额：</text>
								<text>{{ item.totalprice }}</text>
							</view>
							<view class="row">
								<text>本单收益：</text>
								<text>{{ item.sell_price }}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>

		<!-- <view class="data-list">
			<view
				class="order_item"
				v-for="(item,index) in list"
				:key="index">
				<view class="order_head">
					<view class="order_shop">
						<view class="shop_name">订单号：{{item.ordernum}}</view>
						<uni-icons type="forward" size="16" color="#999999"></uni-icons>
					</view>
					<view class="order_status">{{item.status==0?'待支付':item.status==1?'待发货':item.status==2?'待收货':'已完成'}}
					</view>
				</view>
				<view class="order_goods">
					<view class="" v-if="item.goodsdata && item.goodsdata.length <= 1 ">
						<view class="img" v-if="item.goodsdata[0]">
							<image :src="item.goodsdata[0].pic" mode=""></image>
						</view>
						<view class="goods_info">
							<view class="goods_title">
								{{item.goodsdata[0].name}}
							</view>
							<view class="goods_label">
								{{item.goodsdata[0].ggname}}
							</view>
						</view>
					</view>
					<view class="" v-else>
						<view class="img" v-for="(img,i) in item.goodsdata.slice(0,3)" :key="i">
							<image :src="img.pic" mode=""></image>
						</view>

						<view class="goods_total">
							<text>共计{{item.goodsdata.length}}件商品</text>
							<uni-icons type="forward" size="16" color="#999999"></uni-icons>
						</view>
					</view>
				</view>
				<view class="order_shipments">
					<view>
						<text>客户名称：{{item.linkman}}</text>
					</view>
					<view>
						<text>手机号码：{{item.tel}}</text>
					</view>
					<view>
						<text>详细地址：{{item.area}}{{item.address}}</text>
					</view>
				</view>
				<view class="order_footer">
					<view class="order_total">
						<text>共{{item.goodsdata.length}}件商品 合计：</text>
						<text class="font_min">¥</text>
						<text class="font_max">{{orderType===4?item.refund_money: item.totalprice }}</text>
						<text>(含运费{{item.freight_price}})</text>
					</view>

					<view class="btn" v-if="orderType==1">
						<view class="red_btn" @click="onShipments(item.id,item.ordertype, item)">确认发货</view>
					</view>
					<view class="cause" v-if="orderType===4">申请原因：{{item.remark}}</view>
					<view class="btn" v-if="orderType===4&&item.refund_status==1&&(item.status==1||item.status==2)">
						<view style="color: #999999;border:1px solid #999999"
							@click="onMerchantOrderAfterSale('拒绝',item.id,item.ordertype)">拒绝</view>
						<view class="red_btn" @click="onMerchantOrderAfterSale('同意退款',item.id,item.ordertype)">同意退款
						</view>
					</view>
					<view class="" v-else></view>
				</view>
			</view>
			<view class="flex-center addressImgBox" v-if="list.length==0">
				<image src="https://wx.2024csnl.com/static/noData.png"></image>
				暂无数据~
			</view>
			<view style="text-align: center;color: #999;" v-if="noMoreData&&list.length!=0">没有更多啦~</view>
		</view> -->
		
		<!-- 填订单 -->
		<uni-popup ref="popup" type="center">
			<view class="modalBox">
				<view class="contentBox">填写订单</view>
				<view class="popCenterBox">
					<input type="text" v-model="logisticsName" placeholder="请输入物流公司名称">
					<input type="text" v-model="logisticsNumber" placeholder="请输入物流单号">
				</view>
				<view class="between modalBtnBox" style="padding :0 50rpx ;">
					<view class="flex-center confirmBtn" @click="close('popup')">取消</view>
					<view class="flex-center " @click="onMerchantOrderShipments()">确认发货</view>
				</view>
			</view>
		</uni-popup>

		<uni-popup ref="timePopup" type="bottom">
			<u-calendar type="dateRange" :startYear="endYear-1" :endYear="endYear" @change="onChangeTime">
			</u-calendar>
		</uni-popup>

	</view>
</template>

<script>
	import {
		merchantOrder,
		merchantOrderShipments,
		merchantOrderAfterSale
	} from "../../api/index.js"
	export default {
		data() {
			return {
				endYear: new Date().getFullYear(),
				time: '时间',
				selectId: '', //选取订单id
				logisticsName: '',
				logisticsNumber: '',
				showModal: '',
				list: [],
				pagenum: 1,
				noMoreData: false,
				bid: '',
				tabs: ['待付款', '待发货', '待收货', '已完成'],
				orderType: 0,
				goodsType: '', //发货会用到
				startime: '',
				endtime: '',
			};
		},
		onLoad(opt) {
			this.orderType = opt.pageType
			this.bid = opt.bid

			if (opt.startime && opt.endtime) {
				this.startime = opt.startime
				this.endtime = opt.endtime
				this.time = opt.startime
			}

			this.onGetMerchantOrder()
		},
		onReachBottom() {
			if (this.pagenum < this.totalPage) {
				this.pagenum += 1
				this.onGetMerchantOrder('bottom')
			} else {
				this.noMoreData = true
			}
		},
		methods: {
			// 选择时间
			onChangeTime(e) {
				console.log(e, "eee")
				this.startime = e[0].result
				this.endtime = e[1].result
				this.time = e[0].result

				console.log(this.startime)
				console.log(this.endtime)
				this.onRefresh()
				this.close('timePopup')

			},
			// 售后
			async onMerchantOrderAfterSale(type, id, ordertype) {
				// 这段判断临时加的
				if (type == '同意退款') {
					uni.showModal({
						title: '提示',
						content: `平台暂未开通退款,请联系客服`,
						confirmColor: '#41644A',
						showCancel: true,
						confirmText: '联系客服',
						success: (msg) => {
							if (msg.confirm) {
								this.callPhone('15386436268')
							}
						}
					});
					return
				}
				const result = await new Promise(resolve => {
					uni.showModal({
						title: '提示',
						content: `确认${type}？`,
						confirmColor: '#41644A',
						success(msg) {
							resolve(msg)
						}
					});
				})
				if (result.confirm) {
					const res = await merchantOrderAfterSale('s=/ApiBusiness/bOrderrefund', {
						id: id,
						type: type == '拒绝' ? 2 : 1,
						ordertype: ordertype
					})
					const {
						status,
						msg
					} = res.data
					this.showToast(msg)
					if (status == 1) {
						setTimeout(() => {
							this.onRefresh()
						}, 2000)
					}
				}


			},
			onShipments(id, type, item) {
				this.selectId = id
				this.goodsType = type

				if (item.s_express_com) {
					this.logisticsName = item.s_express_com
				} else {
					this.logisticsName = "";
				}
				if (item.s_express_no) {
					this.logisticsNumber = item.s_express_no
				} else {
					this.logisticsNumber = "";
				}
				this.open('popup')
			},
			open(n) {
				this.$refs[n].open();
			},
			close(n) {
				this.$refs[n].close();
			},
			// 发货
			async onMerchantOrderShipments() {
				if (this.logisticsName == '') {
					this.showToast('请输入物流公司名称')
					return
				}
				if (this.logisticsNumber == '') {
					this.showToast('请输入物流单号')
					return
				}
				const res = await merchantOrderShipments('s=/ApiBusiness/orderfh', {
					id: this.selectId, //订单id
					ordertype: this.goodsType,
					express_com: this.logisticsName, //物流公司名字
					express_no: this.logisticsNumber, //物流订单号
				})
				console.log(res, 'rrrrr')
				const {
					status,
					msg
				} = res.data
				this.showToast(msg)
				if (status == 1) {
					setTimeout(() => {
						this.selectId = ''
						this.close('popup')
						this.onRefresh()
					}, 2000)
				}
			},
			onRefresh() {
				this.pagenum = 1
				this.list = []
				this.onGetMerchantOrder()
			},
			async onGetMerchantOrder(type) {
				const res = await merchantOrder('s=/ApiBusiness/businessOrder', {
					bid: this.bid,
					status: this.orderType,
					pagenum: this.pagenum,
					starttime: this.startime,
					endtime: this.endtime
				})
				const {
					status,
					msg,
					data
				} = res.data
				if (status == 1) {
					this.totalPage = data.last_page
					type == 'bottom' ? this.list.push(...data.data) : this.list = data.data
					this.pagenum != 1 && data.data.length == 0 ? this.noMoreData = true : this.noMoreData = false
				} else {
					this.showToast(msg)
				}
			},
			changeTabs(i) {
				this.orderType = i
				this.startime = ''
				this.endtime = ''
				this.time = '时间'
				this.onRefresh()
			},
			linkTo(item, url) {
				uni.navigateTo({
					url: `/package/mine/${url}/${url}?id=${item.id}`
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		position: relative;
		min-height: 100vh;
		background: #f8f8f8;
	}
	
	.page-tabs {
		position: sticky;
		top: 0;
		z-index: 100;
		background: #fff;
		border-radius: 0 0 20rpx 20rpx;
		.fo {
			position: relative;
			display: flex;
			align-items: center;
			.scroll {
				flex: 1;
				height: 100rpx;
				.tabs {
					display: flex;
					align-items: center;
					.item {
						flex: 1;
						&.is-active {
							.cell {
								&:before {
									content: '';
									position: absolute;
									left: 50%;
									top: 60rpx;
									width: 120rpx;
									margin-left: -60rpx;
									height: 14rpx;
									border-radius: 20rpx;
									background: -webkit-linear-gradient(left, #FDD69F, #fff);
								}
								text {
									font-weight: bold;
									color: #000;
								}
							}
						}
					}
					.cell {
						position: relative;
						text {
							position: relative;
							z-index: 2;
							display: block;
							height: 100rpx;
							line-height: 100rpx;
							text-align: center;
							font-size: 30rpx;
							color: #999;
						}
					}
				}
			}
			.filter {
				width: 160rpx;
				height: 100rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				text {
					font-size: 30rpx;
					margin-left: 10rpx;
				}
				image {
					display: block;
					width: 30rpx;
					height: 30rpx;
				}
			}
		}
		.state {
			display: flex;
			align-items: center;
			position: relative;
			background: #fff;
			margin: 0 20rpx;
			padding: 10rpx 0;
			border-top: 1px solid #eee;
			.item {
				flex: 1;
				&.is-active {
					.cell {
						color: #000;
						font-weight: bold;
					}
				}
			}
			.cell {
				position: relative;
				border: 1px solid #ddd;
				margin: 10rpx;
				padding: 10rpx;
				text-align: center;
				border-radius: 20rpx;
				color: #999;
				.num {
					line-height: 40rpx;
					font-size: 30rpx;
				}
				.label {
					line-height: 40rpx;
				}
			}
		}
	}
	
	.page-list {
		.list {}
		.item {
			position: relative;
			&.is-fukuan {
				.cell .state {
					color: #E76730;
				}
			}
			&.is-hexiao,
			&.is-fahuo {
				.cell .state {
					color: #E7A430;
				}
			}
			&.is-shouhuo {
				.cell .state {
					color: #999;
				}
			}
		}
		.cell {
			display: flex;
			align-items: center;
			margin: 20rpx;
			padding: 20rpx;
			background: #fff;
			border-radius: 20rpx;
			.cover {
				image {
					display: block;
					width: 180rpx;
					height: 180rpx;
					background: #eee;
					border-radius: 10rpx;
				}
			}
			.exp {
				flex: 1;
				margin: 0 0 0 20rpx;
			}
			.t1 {
				display: flex;
				align-items: center;
				margin: 0 0 20rpx;
			}
			.name {
				flex: 1;
				line-height: 40rpx;
				font-weight: bold;
				font-size: 30rpx;
			}
			.state {
				text-align: right;
				line-height: 40rpx;
				font-size: 30rpx;
				font-weight: bold;
				color: #999;
			}
			.row {
				margin: 10rpx 0;
				line-height: 30rpx;
				color: #999;
			}
		}
	}
</style>